﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;

namespace EFong.Contract
{
    [DataContract]
    public class TrainTicketOrder : Order
    {
        public TrainTicketOrder() 
        {
            OrderType = "火车票";
            CustomerStatus = "未收款";
            VendorStatus = "未付款";
        }

        private Guid _id = Guid.NewGuid();
        [DataMember]
        public Guid ID
        {
            get { return _id; }
            set { _id = value; }
        }

        private string _ref = string.Empty;
        [DataMember]
        public string Ref
        {
            get { return _ref; }
            set { _ref = value; }
        }

        private string _address = string.Empty;
        [DataMember]
        public string Address
        {
            get { return _address; }
            set { _address = value; }
        }

        private string _orderType = string.Empty;
        [DataMember]
        public string OrderType
        {
            get { return _orderType; }
            set { _orderType = value; }
        }

        private User _orderUser;
        [DataMember]
        public User OrderUser
        {
            get { return _orderUser; }
            set { _orderUser = value; }
        }

        private string _orderUserText = string.Empty;
        [DataMember]
        public string OrderUserText
        {
            get { return _orderUserText; }
            set { _orderUserText = value; }
        }

        private DateTime? _orderDate = DateTime.Now;
        [DataMember]
        public DateTime? OrderDate
        {
            get { return _orderDate; }
            set { _orderDate = value; }
        }

        private string _remark = string.Empty;
        [DataMember]
        public string Remark
        {
            get { return _remark; }
            set { _remark = value; }
        }

        private Customer _customer;
        [DataMember]
        public Customer Customer
        {
            get { return _customer; }
            set { _customer = value; }
        }

        private string _customerText = string.Empty;
        [DataMember]
        public string CustomerText
        {
            get { return _customerText; }
            set { _customerText = value; }
        }

        private string _paymentType = "现结";
        [DataMember]
        public string PaymentType
        {
            get { return _paymentType; }
            set { _paymentType = value; }
        }

        private BankAccount _bankAccount;
        [DataMember]
        public BankAccount BankAccount
        {
            get { return _bankAccount; }
            set { _bankAccount = value; }
        }

        //private string _address = string.Empty;
        //[DataMember]
        //public string Address
        //{
        //    get { return _address; }
        //    set { _address = value; }
        //}

        private string _contactPerson = string.Empty;
        [DataMember]
        public string ContactPerson
        {
            get { return _contactPerson; }
            set { _contactPerson = value; }
        }

        //private string _contactPersonTel = string.Empty;
        //[DataMember]
        //public string ContactPersonTel
        //{
        //    get { return _contactPersonTel; }
        //    set { _contactPersonTel = value; }
        //}

        public string ContactPersonTel
        {
            get { return string.Format("{0}${1}", 联系电话, 移动电话); }
            set
            {
                string[] strs = value.Split('$');
                if (strs.Length == 2)
                {
                    联系电话 = strs[0];
                    移动电话 = strs[1];
                }
            }
        }

        private string _联系电话 = string.Empty;
        [DataMember]
        public string 联系电话
        {
            get { return _联系电话; }
            set { _联系电话 = value; }
        }

        private string _移动电话 = string.Empty;
        [DataMember]
        public string 移动电话
        {
            get { return _移动电话; }
            set { _移动电话 = value; }
        }

        private string _creditTerm = string.Empty;
        [DataMember]
        public string CreditTerm
        {
            get { return _creditTerm; }
            set { _creditTerm = value; }
        }

        private User _orderTicketUser;
        [DataMember]
        public User OrderTicketUser
        {
            get { return _orderTicketUser; }
            set { _orderTicketUser = value; }
        }

        private User _provideTicketUser;
        [DataMember]
        public User ProvideTicketUser
        {
            get { return _provideTicketUser; }
            set { _provideTicketUser = value; }
        }

        private DateTime _provideTicketDate = DateTime.Now;
        [DataMember]
        public DateTime ProvideTicketDate
        {
            get { return _provideTicketDate; }
            set { _provideTicketDate = value; }
        }

        private DateTime _deliveryDate = DateTime.Now;
        [DataMember]
        public DateTime DeliveryDate
        {
            get { return _deliveryDate; }
            set { _deliveryDate = value; }
        }

        //private int _ticketTotalQty;
        //[DataMember]
        public int TicketTotalQty
        {
            get 
            {
                int result = 0;
                foreach (TrainTicketOrderDetail trainTicketOrderDetail in Details) 
                {
                    result += trainTicketOrderDetail.Qty;
                }

                return result;
            }
            //get { return _ticketTotalQty; }
            //set { _ticketTotalQty = value; }
        }

        //private decimal _totalPrice;
        //[DataMember]
        public decimal? TotalPrice
        {
            get
            {
                decimal result = 0;
                foreach (TrainTicketOrderDetail trainTicketOrderDetail in Details)
                {
                    if (trainTicketOrderDetail.TotalPrice == null)
                        return null;
                    result += trainTicketOrderDetail.TotalPrice.Value;
                }

                return result;
            }

            //get { return _totalPrice; }
            //set { _totalPrice = value; }
        }

        private decimal _totalPriceText;
        [DataMember]
        public decimal TotalPriceText 
        {
            get { return _totalPriceText; }
            set { _totalPriceText = value; }
        }

        //private decimal _totalVendorPrice;
        //[DataMember]
        public decimal? TotalVendorPrice
        {
            get
            {
                decimal result = 0;
                foreach (TrainTicketOrderDetail trainTicketOrderDetail in Details)
                {
                    if (trainTicketOrderDetail.TotalVendorPrice == null)
                        return null;
                    result += trainTicketOrderDetail.TotalVendorPrice.Value;
                }

                return result;
            }
            //get { return _totalVendorPrice; }
            //set { _totalVendorPrice = value; }
        }

        //private decimal _totalProfit;
        //[DataMember]
        public decimal? TotalProfit
        {
            get 
            {
                decimal result = 0;
                foreach(TrainTicketOrderDetail trainTicketOrderDetail in Details)
                {
                    if (trainTicketOrderDetail.Profit == null)
                        return null;

                    result += trainTicketOrderDetail.Profit.Value;
                }

                return result;
            }
            //get { return _totalProfit; }
            //set { _totalProfit = value; }
        }

        private Currency _currency;
        [DataMember]
        public Currency Currency
        {
            get { return _currency; }
            set { _currency = value; }
        }

        private string _voidCause = string.Empty;
        [DataMember]
        public string VoidCause
        {
            get { return _voidCause; }
            set { _voidCause = value; }
        }

        private string _status = "未付款";
        [DataMember]
        public string Status
        {
            get { return _status; }
            set { _status = value; }
        }

        private User _salesman;
        [DataMember]
        public User Salesman
        {
            get { return _salesman; }
            set { _salesman = value; }
        }

        private decimal? _printTotalPrice;
        [DataMember]
        public decimal? PrintTotalPrice
        {
            get { return _printTotalPrice; }
            set { _printTotalPrice = value; }
        }

        private Vendor _vendor;
        [DataMember]
        public Vendor Vendor
        {
            get { return _vendor; }
            set { _vendor = value; }
        }

        private Currency _vendorCurrency;
        [DataMember]
        public Currency VendorCurrency
        {
            get { return _vendorCurrency; }
            set { _vendorCurrency = value; }
        }

        private decimal _exchangeRate = 1;
        [DataMember]
        public decimal ExchangeRate
        {
            get { return _exchangeRate; }
            set { _exchangeRate = value; }
        }

        private List<TrainTicketOrderDetail> _details = new List<TrainTicketOrderDetail>();
        [DataMember]
        public List<TrainTicketOrderDetail> Details
        {
            get { return _details; }
            set { _details = value; }
        }


        private List<RTrainTicketReturn> _trainTicketReturnHis = new List<RTrainTicketReturn>();
        [DataMember]
        public List<RTrainTicketReturn> TrainTicketReturnHis
        {
            get { return _trainTicketReturnHis; }
            set { _trainTicketReturnHis = value; }
        }

        public override bool Equals(object obj)
        {
            TrainTicketOrder temp = obj as TrainTicketOrder;
            if (temp == null)
                return false;
            return (this.ID.Equals(temp.ID));
        }
    }
}
